package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * packageName com.sky.task
 *
 * @author fenglai
 * @version JDK 11
 * @className OrderTask
 * @date 2023/12/5
 * @description TODO
 */
@Component
@Slf4j
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * @param : return void
     * @author fenglai
     * @date 2023/12/5 14:52
     * @description 订单未支付超出时长 进行订单取消
     */
    @Scheduled(cron = "0 1 * * * ?")
    public void doDetectionStatus() {

        List<Orders> orders = orderMapper.getOrdersByStatusAndOrderTime(Orders.PENDING_PAYMENT, LocalDateTime.now().minusMinutes(15));

        if (orders != null && orders.size() > 0) {
            for (Orders order : orders) {
                order.setStatus(Orders.CANCELLED);
                order.setCancelTime(LocalDateTime.now().minusMinutes(15));
                order.setCancelReason("订单未付款已超出时间");
                orderMapper.update(order);
            }

        }

    }


    /**
     * @param :
     * return void
     * @author fenglai
     * @date 2023/12/5 14:59
     * @description 每天凌晨1单 查询12点前的订单状态配送中 并且下单时间是一个小时前的 改为已完成
     */
@Scheduled(cron = "0 0 1 * * ?")
    public void doDeliveryStatus() {

        List<Orders> orders = orderMapper.getOrdersByStatusAndOrderTime(Orders.DELIVERY_IN_PROGRESS, LocalDateTime.now().minusHours(1));

    if (orders != null && orders.size() > 0) {
        for (Orders order : orders) {
            order.setStatus(Orders.COMPLETED);
            order.setDeliveryTime(LocalDateTime.now().minusHours(1));
            orderMapper.update(order);
        }

    }

    }


}